//
//  UIView+category.m
//  XOWMSDK
//
//  Created by SKL on 2017/11/28.
//  Copyright © 2017年 SKL. All rights reserved.
//

#import "UIView+category.h"
//NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX
//                                                           relatedBy:NSLayoutRelationEqual toItem:self.superview
//                                                           attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
//
//NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY
//                                                           relatedBy:NSLayoutRelationEqual toItem:self.superview
//                                                           attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
//
//NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth
//                                                         relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil
//                                                         attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.bounds.size.width];
//
//NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight
//                                                          relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil
//                                                          attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.bounds.size.height];
//[self.superview addConstraints:@[centerX,centerY,width,height]];
@implementation UIView (category)
//- (void)setQualto:(EqualTo)qualto
//{
//    
//    
//}
//
//- (EqualTo)qualto
//{
//    
//    
//}

- (void)yjd_top:(CGFloat)top_float
{
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:top_float];
    [self updateConstraint:top];
}

- (void)yjd_bottom:(CGFloat)bottom_float
{
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:bottom_float];
    [self updateConstraint:bottom];
}

- (void)yjd_left:(CGFloat)left_float
{
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:left_float];
    [self updateConstraint:left];
}

- (void)yjd_right:(CGFloat)right_float
{
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:right_float];
    [self updateConstraint:right];
}

- (void)yjd_width:(CGFloat)width_float
{
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width_float];
    [self updateConstraint:width];
}

- (void)yjd_height:(CGFloat)height_float
{
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height_float];
    [self updateConstraint:height];
}

- (void)yjd_centerX:(CGFloat)centerX_float
{
    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:centerX_float - self.superview.center.x];
    [self updateConstraint:centerX];
}

- (void)yjd_centerY:(CGFloat)centerY_float
{
    NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:centerY_float - self.superview.center.y];
    [self updateConstraint:centerY];
}

- (void)yjd_size:(CGSize)size
{
    [self yjd_width:size.width];
    [self yjd_height:size.height];
}

- (void)yjd_center:(CGPoint)center
{
    [self yjd_centerX:center.x];
    [self yjd_centerY:center.y];
}

- (void)yjd_inset:(UIEdgeInsets)inset
{
    [self yjd_top:inset.top];
    [self yjd_bottom: - inset.bottom];
    [self yjd_left:inset.left];
    [self yjd_right: - inset.right];
}

- (void)yjd_top:(CGFloat)top_float equaltTo:(UIView *)view
{
   NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0 constant:top_float];
    [self updateConstraint:top];
    
}
- (void)updateConstraint:(NSLayoutConstraint *)constraint
{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    for (NSLayoutConstraint *subConstraint in self.superview.constraints)
    {
        if (subConstraint.firstAttribute == constraint.firstAttribute && subConstraint.firstItem == constraint.firstItem && subConstraint.secondItem == constraint.secondItem)
        {
            [self.superview removeConstraint:subConstraint];
        }
    }
    [self.superview addConstraint:constraint];
}
- (UIWindow *)yjd_keyWindow
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window?[UIApplication sharedApplication].delegate.window:[UIApplication sharedApplication].keyWindow;
    NSAssert(window, @"%@没有找到window",NSStringFromClass([self class]));
    return window;
}
- (UIView *)yjd_findKeyBoard
{
    UIView *keyboardView = nil;
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *window in [windows reverseObjectEnumerator])//逆序效率更高，因为键盘总在上方
    {
        keyboardView = [self yjd_findKeyBoardInView:window];
        if (keyboardView)
        {
            return keyboardView;
        }
    }
    return nil;
}
+ (UIWindow *)yjd_keyWindow
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window?[UIApplication sharedApplication].delegate.window:[UIApplication sharedApplication].keyWindow;
    NSAssert(window, @"%@没有找到window",NSStringFromClass([self class]));
    return window;
}
- (UIView *)yjd_findKeyBoardInView:(UIView *)view
{
    for (UIView *subView in [view subviews])
    {
        if (strstr(object_getClassName(subView), "UIKeyboard"))
        {
            return subView;
        }
        else
        {
            UIView *tempView = [self yjd_findKeyBoardInView:subView];
            if (tempView)
            {
                return tempView;
            }
        }
    }
    return nil;
}

- (void)sp_checkUserInfo {
    NSLog(@"Get User Succrss");
}
@end
